import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';

@Module({ namespaced: true, stateFactory: true })
export default class SmsCode extends VuexModule {
  counter = 10; // 倒计时数字
  busy = false; // 是否倒计时状态
  smsLabelText = '短信验证码'; // 验证码label话术
  smsText = '获取验证码'; // 发送短信按钮话术
  timerId = 0; // 倒计时handler
  tartTime = null;

  @Mutation
  setTimerId (timerId: number) {
    this.timerId = timerId
  }

  @Mutation
  countDown () {
    this.counter--;
    this.smsText = `${this.counter}S重新获取`
    if (this.counter === 0) {
      clearInterval(this.timerId);
      this.counter = 0;
      this.smsText = '获取验证码';
    }
  }

  @Action({ rawError: true })
  async startCount () {
    const timerId = setInterval(() => {
      this.context.commit('countDown');
    }, 1000)
    this.context.commit('setTimerId', timerId);
  }
}
